package cn.cvs.controller;

import cn.cvs.pojo.SysRole;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysUserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sun.security.pkcs11.wrapper.Constants;

import javax.annotation.Resource;
import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class SysUserController {
    private static final Logger logger = Logger.getLogger(SysUserController.class.getName());
    private ArrayList<SysUser> sysUserList = new ArrayList<SysUser>();
    private ArrayList<SysUser> userList = new ArrayList<SysUser>();
    private ArrayList<SysUser> queryUserList = new ArrayList<SysUser>();
@Resource
private SysUserService sysUserService;

//跳转到登录页面
    @RequestMapping("/toLogin")
    public String login(){
        logger.debug("跳转到登录页面");

        return "login";
    }
    //登录
    @PostMapping("/login")
    public String doLogin(HttpServletRequest request, HttpSession session, @RequestParam String account, @RequestParam String password){
        logger.debug("登录方法");
        SysUser sysUser= sysUserService.login(account,password);
        if (null!=sysUser) {
            session.setAttribute(Constants.INDENT,sysUser);
            return "redirect:/user/toMain";
        }else {
            request.setAttribute("error","用户名或密码不正确");
            return "login";
        }
    }
    //跳转到首页
    @RequestMapping("/toMain")
    public String main(HttpSession session){
        if (session.getAttribute(Constants.NEWLINE)==null){
            return "redirect:/user/toLogin";
        }
        return "frame";
    }

    public SysUserController() {
        try {
            sysUserList.add(new SysUser(5, "zhouzhou", "赵静", "44444444", 1, "13011111111", "上海宝安区", 1, 1, new Date(), 1, new Date()));
            sysUserList.add(new SysUser(4, "zhouzhou", "赵静", "44444444", 1, "13011111111", "上海宝安区", 1, 1, new Date(), 1, new Date()));
            sysUserList.add(new SysUser(3, "zhouzhou", "赵静", "44444444", 1, "13011111111", "上海宝安区", 1, 1, new Date(), 1, new Date()));
            sysUserList.add(new SysUser(2, "zhouzhou", "赵静", "44444444", 1, "13011111111", "上海宝安区", 1, 1, new Date(), 1, new Date()));
            sysUserList.add(new SysUser(1, "zhouzhou", "赵静", "44444444", 1, "13011111111", "上海宝安区", 1, 1, new Date(), 1, new Date()));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询所有用户
    @GetMapping("/list")
    public String list(Model model) {
        logger.info("当查询条件为空时，查询全部");
        queryUserList.clear();
        queryUserList.addAll(userList);
        model.addAttribute("queryUserList", queryUserList);
        return "sysUser/sysUserList";
    }


    //根据条件查询部分用户
    @RequestMapping("/list")
    public String list(@RequestParam(value = "realName", required = false) String realName, Model model) {
        logger.info("查询条件：[realName]=" + realName + "查询用户信息");
        queryUserList.clear();
        if (realName != null && realName.equals("")) {
            for (SysUser sysUser : userList) {
                queryUserList.add(sysUser);
            }
        }
        model.addAttribute("queryUserList", queryUserList);
        return "sysUser/sysUserList";
    }

    @ExceptionHandler(value = {RuntimeException.class})
    public String handlerException(RuntimeException e,HttpServletRequest request){
        request.setAttribute("e",e);
        return "error";
    }

    //一个抛出异常的接口
    @RequestMapping("/exLogin")
    public String exLogin(@RequestParam String account,@RequestParam String password){
        SysUser sysUser=sysUserService.login(account,password);
        if (null==sysUser) {
            throw new RuntimeException();
        }
        return "redirect:/user/toMain";

    }


    @GetMapping(value = "/list")
    public String getUserList(Model model,String queryRealName,Integer queryRoleId,@RequestParam(defaultValue = "1") Integer pageIndex){
        List<SysUser> userList=null;
        try{
            int pageSize=Constants.pageSize;
            int totalCount=sysUserService.getUserCount(queryRealName,queryRoleId);
            PageSupport pages=new PageSupport();
          pages.setCurrentPageNo(pageIndex);
          pages.setPageSize(pageSize);
          pages.setTotalCount(totalCount);
          int totalPageCount=pages.getTotalPageCount();
            if (pageIndex>totalPageCount) {
                pageIndex=totalPageCount;
                throw new RuntimeException("页码不正确");
            }
            userList=sysUserService.getUserList(queryRealName,queryRoleId,pageIndex,pageSize);
            model.addAttribute("userList",userList);
            List<SysRole> roleList=null;
            model.addAttribute("roleList",roleList);
            model.addAttribute("queryRealName",queryRealName);
            model.addAttribute("queryRoleId",queryRoleId);
            model.addAttribute("totalPageCount",totalPageCount);
            model.addAttribute("totalCount",totalCount);
            model.addAttribute("currentPageNo",pageIndex);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("用户列表接口访问失败");
            return "redirect:/user/toSysError";

        }
        return "redirect:/list";
    }

    @RequestMapping("/toSysError")
    public String sysError(){
        return "sysError";
    }
}
